<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row type="flex">
          <!-- 搜索 -->
          <span class="tlie">关键字</span>
          <el-form>
            <el-form-item>
              <el-input
                style="width: 200px"
                class="filter-item"
                v-model="requestParameters.keyword"
                placeholder="请根据编号搜索"
              ></el-input>
            </el-form-item>
            <div class="blak"></div>
          </el-form>
          <div class="blank"></div>
          <el-button size="small" @click="requestParameters.keyword = ''">清除</el-button>
          <el-button type="primary" @click="Check()" size="small">搜索</el-button>
        </el-row>
        <el-alert type="info" class="alert" :closable="false" show-icon>数据一共{{ counts }} 条</el-alert>
        <!-- end -->
        <!-- 数据 -->
        <el-table fit :data="list" highlight-current-row style="width: 100%">
          <el-table-column prop="id" label="编号"></el-table-column>
          <el-table-column label="题型">
            <template slot-scope="scope">
              <span>
                {{
                scope.row.questionType === "2" ? "多选" : "简答"
                }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="题目编号">
            <template slot-scope="scope">
              <span v-for="(obj,index) in scope.row.questionIDs" :key="index">
                <el-button type="text" @click="hanldPreviewQusetion(obj.id)">{{ obj.number }}</el-button>
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间"></el-table-column>
          <el-table-column prop="totalSeconds" label="答题时间(s)"></el-table-column>
          <el-table-column prop="accuracyRate" label="正确率(%)"></el-table-column>
          <el-table-column prop="userName" label="录入人"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="danger"
                @click="delItemalog(scope.row.id)"
                icon="el-icon-delete"
                circle
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- end -->
        <!-- 分页 -->
        <div class="pagination">
          <div class="pages">
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="requestParameters.page"
              :total="counts"
              :page-size="requestParameters.pagesize"
              :page-sizes="[20, 30, 50, 100]"
              layout="sizes, prev, pager, next, jumper"
            ></el-pagination>
          </div>
        </div>
        <preview-dialog v-if="previewQes" ref="previewQuestion" :formBase="formData"></preview-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
// 接口
import { randoms, removeRandoms, detail } from '../../api/hmmm/questions'
// 弹出层
import previewDialog from '../components/questions-preview.vue'

export default {
  name: 'list',
  components: {
    previewDialog
  },
  data() {
    return {
      list: [],
      item: [],
      counts: 0,
      formData: {},
      previewQes: false,
      requestParameters: {
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      listLoading: true,
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.getrandomsList()
  },
  methods: {
    // 删除弹框
    delItemalog(id) {
      this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        },
        this.delItem(id)
      )
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 查询
    Check() {
      this.requestParameters.page = 1
      this.getrandomsList()
    },
    // 列表数据请求
    async getrandomsList() {
      this.listLoading = true
      const { data } = await randoms(this.requestParameters)
      // 打印数据
      // console.log('---data', data)
      // 获取总条数
      this.counts = data.counts
      // 获取数据给List
      this.list = data.items
      this.listLoading = false
    },
    // 删除
    async delItem(id) {
      await removeRandoms({ id })
      this.getrandomsList()
    },
    // 每页显示信息条数
    handleSizeChange(val) {
      this.requestParameters.pagesize = val
      if (this.requestParameters.page === 1) {
        this.getrandomsList(this.requestParameters)
      }
    },
    // 进入某一页
    handleCurrentChange(val) {
      this.requestParameters.page = val
      this.getrandomsList()
    },
    // 题目预览数据
    async hanldPreviewQusetion(id) {
      this.formData.id = id
      const { data: res } = await detail({ id: id })
      this.formData = res
      this.previewQes = true
      this.$nextTick(() => {
        this.$refs.previewQuestion.dialogFormV()
      })
    }
  }
}
</script>
<style  lang="less" scoped>
.alert {
  margin: 10px 0px;
}
.pages {
  float: right;
  padding: 10px;
}
.blank {
  width: 60vw;
}
.tlie {
  display: inline-block;
  font-size: 14px;
  margin-top: 12px;
  margin-right: 7px;
}
.el-button {
  height: 30px;
}
</style>
